package cn.edu.zhku.citrusmonitor.baseinfo.technology.service;

import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.zhku.citrusmonitor.baseinfo.technology.dao.TechnologyDao;
import cn.edu.zhku.citrusmonitor.baseinfo.technology.dao.TechnologyDaoImpl;
import cn.edu.zhku.citrusmonitor.baseinfo.technology.exception.TechnologyException;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbTechnology;
import cn.edu.zhku.citrusmonitor.util.PageBean;

public class TechnologyServiceImpl implements TechnologyService{

	private TechnologyDao tDao = new TechnologyDaoImpl();
	
	@Override
	public boolean addTechnology(TbTechnology technology) throws TechnologyException {
		try {
			int effectedNum = tDao.insertTechnology(technology);
			if (effectedNum > 0) {
				return true;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("添加信息失败，请重试!");
		}
		return false;
	}

	@Override
	public boolean modifyTechnology(TbTechnology technology) throws TechnologyException {
		try {
			int effectedNum = tDao.modifyTechnology(technology);
			if (effectedNum > 0) {
				return true;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("更新信息失败，请重试!");
		}
		return false;
	}

	@Override
	public boolean removeTechnology(String technologyId) throws TechnologyException {
		try {
			String absolutePath = tDao.selectFilePathById(technologyId);
			int effectedNum = 0;
			if(absolutePath == null) {
				effectedNum = tDao.deleteTechnology(technologyId);
				if (effectedNum > 0) {
					return true;
				}
			}else {
				File file = new File(absolutePath);
				effectedNum = tDao.deleteTechnology(technologyId);
				if (effectedNum > 0) {
					if(file.exists()) {
						file.delete();
					}
					return true;
				}
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("删除信息失败，请重试!");
		}
		return false;
	}

	@Override
	public TbTechnology findTechnologyById(String technologyId) throws TechnologyException {
		try {
			return tDao.selectTechnologyById(technologyId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("查询信息失败，请重试!");
		}
	}

	@Override
	public List<TbTechnology> findTechnologyByCategory(String category) throws TechnologyException {
		try {
			return tDao.selectTechnologyByCategory(category);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("查询信息失败，请重试!");
		}
	}

	@Override
	public PageBean<TbTechnology> findTechnologyByCategoryLike(int pageSize, int currentPage, String category) throws TechnologyException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("category", category);
		try {
			return fySearchByCondition(pageSize, currentPage, map);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("查询信息失败，请重新!");
		}
	}

	@Override
	public PageBean<TbTechnology> fySearchAll(int pageSize, int currentPage) throws TechnologyException {
		try {
			return fySearchByCondition(pageSize, currentPage, null);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("查询信息失败，请重新!");
		}
	}
	
	private PageBean<TbTechnology> fySearchByCondition(int pageSize, int currentPage, Map<String, Object> condition) throws SQLException {
		PageBean<TbTechnology> pageBean = new PageBean<TbTechnology>();
		int count = tDao.countList(condition);
		List<TbTechnology> techList = tDao.selectTechnologyList(pageSize, currentPage, condition);
		pageBean.setCurrentPage(currentPage);
		pageBean.setList(techList);
		pageBean.setCount(count);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage((int)Math.ceil(count*1.0/pageSize));
		return pageBean;
	}

	@Override
	public List<TbTechnology> findTechnologyByCategoryLimit(int size, String category) throws TechnologyException {
		try {
			return tDao.selectTechnologyByCategoryLimit(size, category);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("加载失败，请重试!");
		}
	}

	@Override
	public PageBean<Bean> findTechnologyPageByCategory(int pageSize, int currentPage, String category) throws TechnologyException {
		PageBean<Bean> pageBean = new PageBean<Bean>();
		int count;
		try {
			count = tDao.countList(category);
			List<Bean> techList = tDao.selectTechnologyList(pageSize, currentPage, category);
			pageBean.setCurrentPage(currentPage);
			pageBean.setList(techList);
			pageBean.setCount(count);
			pageBean.setPageSize(pageSize);
			pageBean.setTotalPage((int)Math.ceil(count*1.0/pageSize));
			return pageBean;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("加载失败，请重试!");
		}
	}

	@Override
	public Bean getTechnologyById(String technologyId) throws TechnologyException {
		try {
			return tDao.getTechnologyById(technologyId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("查询信息失败，请重试!");
		}
	}
	
	@Override
	public boolean removeTechnologysByIds(String[] ids) throws TechnologyException {
		Object[][] params = new Object[ids.length][];
		for(int i=0;i<ids.length;i++) {
			params[i] = new Object[] {ids[i]};
		}
		try {
			int res = tDao.deleteTechnologysByIds(params);
			if(res == ids.length) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TechnologyException("删除失败，请重试!");
		}
		return false;
	}

}